package com.xiaodou.mjf.infrastructure.common.sms;


import com.xiaodou.mjf.infrastructure.common.utils.PropertiesLoader;

import java.net.URLEncoder;
import java.util.Properties;

/**
 * Created by Rocky on 14-9-19.
 */
public class SMSClient {

    final static String SN = "SDK-WSS-010-07908";
    final static String PASSWORD = "b0bd9ee@";

    public static final String TYPE_VALIDATECODE = "validatecode";
    public static final String TYPE_PAYMENT_CODE = "paymentcode";
    public static final String TYPE_BINGDING_CODE = "bingdingcard";
    public static final String TYPE_WELCOME = "welcome";
    public static final String TYPE_NEW_USER = "newuser";
    public static final String TYPE_REDEEM_CODE = "redeemcode";

    static Properties props = new PropertiesLoader("sms.properties").getProperties();

    private static boolean send(String type,String code,String mobile){
        try {

            SmsWebServiceClient client = new SmsWebServiceClient(SN,PASSWORD);
            String message = props.getProperty(type);
            message = message.replace("#{code}",code);
            message = URLEncoder.encode(message, "utf-8");
            String result = client.mdsmssend(mobile,message, "","","","");
            System.out.println(result);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public static boolean sendValidateCode(String code,String mobile){
    	return SMSClient.send(TYPE_VALIDATECODE, code, mobile);
    }

    public static boolean sendPaymentCode(String code,String mobile){
        return SMSClient.send(TYPE_PAYMENT_CODE, code, mobile);
    }

    /**
     * 发送绑卡验证码
     * @param code 验证码
     * @param mobile 手机号
     * @param cardNumber 卡号后四位
     * @return
     */
    public static boolean sendBindingCardCode(String code,String mobile,String cardNumber){
        try {

            SmsWebServiceClient client = new SmsWebServiceClient(SN,PASSWORD);
            String message = props.getProperty(TYPE_BINGDING_CODE);
            message = message.replace("#{code}",code);
            message = message.replace("#{card}",cardNumber);
            message = URLEncoder.encode(message, "utf-8");
            String result = client.mdsmssend(mobile,message, "","","","");
            System.out.println(result);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean sendNewUserMessage(String mobile,String url){
        try {

            SmsWebServiceClient client = new SmsWebServiceClient(SN,PASSWORD);
            String message = props.getProperty(TYPE_NEW_USER);
            message = message.replace("#{mobile}",mobile);
            message = message.replace("#{url}",url);
            message = URLEncoder.encode(message, "utf-8");
            String result = client.mdsmssend(mobile,message, "","","","");
            System.out.println(result);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean sendRedeemCode(String mobile,String project,String service,String code){
        try {

            SmsWebServiceClient client = new SmsWebServiceClient(SN,PASSWORD);
            String message = props.getProperty(TYPE_REDEEM_CODE);
            message = message.replace("#{project}",project);
            message = message.replace("#{service}",service);
            message = message.replace("#{code}",code);
            message = URLEncoder.encode(message, "utf-8");
            String result = client.mdsmssend(mobile,message, "","","","");
            System.out.println(result);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean sendRedeemCode(String mobile,Double amount,String service,String code){
        try {

            SmsWebServiceClient client = new SmsWebServiceClient(SN,PASSWORD);
            String message = props.getProperty(TYPE_REDEEM_CODE);
            message = message.replace("#{amount}",String.valueOf(amount));
            message = message.replace("#{activity}",service);
            message = message.replace("#{code}",code);
            message = URLEncoder.encode(message, "utf-8");
            String result = client.mdsmssend(mobile,message, "","","","");
            System.out.println(result);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



    public static boolean sendWelcome(String mobile){
        return SMSClient.send(TYPE_WELCOME, "", mobile);
    }



    public  static void main(String[] args){
        //SMSClient.sendValidateCode("984024","18149792047");
        //SMSClient.sendValidateCode("984024","18521351306");
        //SMSClient.sendValidateCode("984024","13661634728");
        //SMSClient.send(SMSClient.TYPE_VALIDATECODE,"123463","18037033110");
        //SMSClient.send(SMSClient.TYPE_VALIDATECODE,"123463","15779926111");
//        SMSClient.sendValidateCode("984024","13661634728");
//        SMSClient.sendValidateCode("984024","18521351306");
//        SMSClient.sendValidateCode("984024","18149792047");
//        SMSClient.sendValidateCode("984024","13761839351");
        //SMSClient.sendNewUserMessage("13661634728","http://www.xiaodoue.com");
        //SMSClient.sendRedeemCode("18521351306","瑞士原产-瑞兰玻尿酸","瑞兰玻尿酸1支","138779022887");
        SMSClient.sendRedeemCode("18521351306",300D,"瑞兰玻尿酸1支","138779022887");
    }
}
